package com.fstech.whiteboadrd.dialog;

import android.animation.ValueAnimator;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.animation.LinearInterpolator;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fstech.whiteboadrd.R;
import com.fstech.whiteboadrd.control.ControlManager;
import com.fstech.whiteboadrd.lib.MultiImageSelectorFragment;
import com.fstech.whiteboadrd.lib.Utils.ScreenUtils;
import com.fstech.whiteboadrd.utils.UIUtils;
import com.squareup.picasso.Picasso;

import static com.fstech.whiteboadrd.dialog.DialogManager.DIALOG_TYPE.SAVE_FIRST;
import static com.fstech.whiteboadrd.dialog.DialogManager.DIALOG_TYPE.SAVE_LAST;
import static com.fstech.whiteboadrd.dialog.DialogManager.DIALOG_TYPE.SAVE_SECOND;
import static com.fstech.whiteboadrd.dialog.DialogManager.DIALOG_TYPE.SAVE_THIRD;

public class DialogManager extends Dialog implements View.OnClickListener {

    private TextView tipContent;
    private TextView save_path;
    private EditText dialogFilename;
    private CheckBox dialogFiletype1;
    private LinearLayout dialogTwo;
    private LinearLayout dialogThree;
    private LinearLayout panelAction;
    private TextView savePath;
    private TextView tipCancel;
    private TextView tipSure;
    private View dialog_one;
    private View dialog_view;
    private ImageView iv_saving;
    private ImageView dialog_qrcode;
    private TextView dialog_title;
    private FrameLayout dialog_save;
    private LinearLayout dialog_llqrcode;
    private View dialog_view1;

    private void initView() {
        tipContent = (TextView) findViewById(R.id.tip_content);
        save_path = (TextView) findViewById(R.id.save_path);
        dialogFilename = (EditText) findViewById(R.id.dialog_filename);
        dialogFiletype1 = (CheckBox) findViewById(R.id.dialog_filetype1);
        dialogTwo = (LinearLayout) findViewById(R.id.dialog_two);
        dialogThree = (LinearLayout) findViewById(R.id.dialog_three);
        panelAction = (LinearLayout) findViewById(R.id.panel_action);
        savePath = (TextView) findViewById(R.id.save_path);
        tipCancel = (TextView) findViewById(R.id.tip_cancel);
        tipSure = (TextView) findViewById(R.id.tip_sure);
        dialog_one = findViewById(R.id.dialog_one);
        dialog_view = findViewById(R.id.dialog_view);
        iv_saving = findViewById(R.id.iv_saving);

        dialog_view1 = findViewById(R.id.dialog_view1);

        dialog_llqrcode = findViewById(R.id.dialog_llqrcode);
        dialog_qrcode = findViewById(R.id.dialog_qrcode);
        dialog_title = findViewById(R.id.dialog_title);
        dialog_save = findViewById(R.id.dialog_save);
    }

    public enum DIALOG_TYPE {
        SAVE_FIRST, SAVE_SECOND, SAVE_THIRD, SAVE_LAST, SCAN
    }

    private DIALOG_TYPE mCurrentType;

    public DialogManager(@NonNull Context context) {
        super(context);
        init();
    }

    public DialogManager(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        init();
    }

    protected DialogManager(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init();
    }

    private void init() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.layout_dialog);
        setCanceledOnTouchOutside(false);
        setCancelable(true);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        initView();
        initEvent();
    }

    private void initEvent() {
        tipCancel.setOnClickListener(this);
        tipSure.setOnClickListener(this);
        findViewById(R.id.dialog_close).setOnClickListener(this);
    }

    private void showSaveFirst() {
        mCurrentType = SAVE_FIRST;
        tipCancel.setText(R.string.cancel);
        tipSure.setText(R.string.next);
        tipCancel.setTextColor(getContext().getResources().getColor(R.color.tv_gray3));
        tipSure.setTextColor(getContext().getResources().getColor(R.color.blue));
        tipContent.setVisibility(View.VISIBLE);
        panelAction.setVisibility(View.VISIBLE);
        dialogTwo.setVisibility(View.GONE);
        dialogThree.setVisibility(View.GONE);
        dialog_one.setVisibility(View.GONE);
        dialog_view.setVisibility(View.VISIBLE);
    }

    private void showSaveSecond() {
        mCurrentType = SAVE_SECOND;
        tipCancel.setText(R.string.cancel);
        tipSure.setText(R.string.save);
        tipCancel.setTextColor(getContext().getResources().getColor(R.color.tv_gray3));
        tipSure.setTextColor(getContext().getResources().getColor(R.color.blue));
        tipContent.setVisibility(View.GONE);
        dialogTwo.setVisibility(View.GONE);
        dialogThree.setVisibility(View.GONE);
        dialog_one.setVisibility(View.VISIBLE);
        panelAction.setVisibility(View.VISIBLE);
        dialog_view.setVisibility(View.VISIBLE);
        dialogFilename.requestFocus();
        ScreenUtils.showInput(dialogFilename);
    }

    private void showSaveThird() {
        ScreenUtils.hideInput(dialogFilename);
        mCurrentType = SAVE_THIRD;
        tipContent.setVisibility(View.GONE);
        dialogTwo.setVisibility(View.VISIBLE);
        dialogThree.setVisibility(View.GONE);
        dialog_one.setVisibility(View.GONE);
        panelAction.setVisibility(View.INVISIBLE);
        dialog_view.setVisibility(View.INVISIBLE);
        startAnimate();
    }

    public void showSaveLast(String path) {
        mCurrentType = SAVE_LAST;
        save_path.setText(path);
        tipCancel.setText(R.string.continuely);
        tipSure.setText(R.string.quit);
        tipCancel.setTextColor(getContext().getResources().getColor(R.color.tv_gray3));
        tipSure.setTextColor(getContext().getResources().getColor(R.color.tv_gray3));
        tipContent.setVisibility(View.GONE);
        dialogTwo.setVisibility(View.GONE);
        dialogThree.setVisibility(View.VISIBLE);
        dialog_one.setVisibility(View.GONE);
        panelAction.setVisibility(View.VISIBLE);
        dialog_view.setVisibility(View.VISIBLE);

    }

    @Override
    public void dismiss() {
        mCurrentType = SAVE_FIRST;
        super.dismiss();
    }

    public void show(DIALOG_TYPE type) {
        hideScan();
        switch (type) {
            case SAVE_FIRST:
                showSaveFirst();
                break;
            case SAVE_SECOND:
                showSaveSecond();
                break;
            case SAVE_THIRD:
                showSaveThird();
                break;
            case SAVE_LAST:
//                showSaveLast(path);
                break;
            case SCAN:
//                showSaveLast(path);
                break;
            default:
                break;
        }
        show();
    }

    @Override
    public void show() {
        if (isShowing()) {
            dismiss();
        }
        super.show();
    }

    private void hideScan() {
        dialog_save.setVisibility(View.VISIBLE);
        dialog_title.setVisibility(View.VISIBLE);
        panelAction.setVisibility(View.VISIBLE);
        dialog_llqrcode.setVisibility(View.GONE);
        dialog_view.setVisibility(View.VISIBLE);
        dialog_view1.setVisibility(View.VISIBLE);
    }

    public void showScan(String url) {
        Log.e("yx", "showScan");
        dialog_save.setVisibility(View.GONE);
        panelAction.setVisibility(View.GONE);
        dialog_title.setVisibility(View.GONE);
        dialog_llqrcode.setVisibility(View.VISIBLE);
        dialog_view.setVisibility(View.GONE);
        dialog_view1.setVisibility(View.GONE);
        // 显示图片
        Picasso.with(UIUtils.getContext())
                .load(url)
                .placeholder(R.drawable.default_error)
                .into(dialog_qrcode);
        show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tip_sure:
                onClick(false);
                break;
            case R.id.tip_cancel:
                onClick(true);
                break;
            case R.id.dialog_close:
                dismiss();
                break;
        }
    }

    /**
     * @param isCancel 是否是cancel按钮被点击了
     */
    private void onClick(boolean isCancel) {
        if (isCancel) {
            dismiss();
            return;
        }
        switch (mCurrentType) {
            case SAVE_FIRST:
                showSaveSecond();
                break;
            case SAVE_SECOND:
                String name = dialogFilename.getText().toString();
                if (TextUtils.isEmpty(name)) {
                    UIUtils.showToastSafe(R.string.name_empty_tip);
                } else {
                    if (mCallback != null) {
                        mCallback.onSave(name);
                    } else {
                        dismiss();
                    }
                }
                break;
            case SAVE_THIRD:
//                showSaveLast(path);
                break;
            case SAVE_LAST:
                if (mCallback != null) {
                    mCallback.onquit();
                }
                break;
            default:
                break;
        }
    }


    private void startAnimate() {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 360);

        valueAnimator.setDuration(1200);

        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);

        valueAnimator.setInterpolator(new LinearInterpolator());

        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                iv_saving.setRotation((Float) animation.getAnimatedValue());
            }
        });
        valueAnimator.start();
    }

    ControlManager.ControlCallback mCallback;

    public void setCallback(ControlManager.ControlCallback callback) {
        mCallback = callback;
    }

}
